<template>
	<view class="pics">
		<scroll-view class="left" scroll-y>
			<view :class="[active === index ? 'active' : '']" 
				v-for="(item, index) in cates"
				:key="index"
				@click="selectCate(index, item.id)"
			>
				{{item.title}}
			</view>
		</scroll-view>
		
		<scroll-view scroll-y="true" class="right">
			<view class="item" v-for="item in pics" :key="item.id">
				<image @click="previewPic(item.img_url)" :src="item.img_url"></image>
				<text>{{item.title}}</text>
			</view>
			<text class="white" v-if="pics.length === 0">暂无数据</text>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				active: 0,
				cates: [],
				pics: [],
			}
		},
		methods: {
			async getCates() {
				const res = await this.$myRequest({
					url: '/api/getimgcategory'
				})
				this.cates = res.data.message
				this.selectCate(0, this.cates[0].id)
			},
				// 获取右侧图片数据
			async selectCate(index, id) {
				this.active = index;
				const res = await this.$myRequest({
					url: '/api/getimages/' + id
				})
				this.pics = res.data.message
			},
			
			previewPic(current) {
				const urls = this.pics.map(item => {
					return item.img_url
				})
				uni.previewImage({
					current,
					urls
				})
			}
		},
		onLoad() {
			this.getCates()
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}
	.pics {
		height: 100%;
		display: flex;
		.left {
			width: 200rpx;
			height: 100%;
			border-right: 1px solid #eee;
			margin-top: 30rpx;
			view {
				height: 60px;
				line-height: 60px;
				text-align: center;
				font-size: 30rpx;
				color: #333;
				border-bottom: 1px solid #eee;
			}
			.active {
				background-color: $shop-color;
				color: #fff;
			}
		}
		.right {
			height: 100%;
			width: 510rpx;
			margin: 0 auto;
			.item {
				image {
					width: 510rpx;
					height: 510rpx;
					border-radius: 5px;
					margin-top: 30rpx;
				}
				text {
					font-size: 30rpx;
					line-height: 60rpx;
				}
			}
			.white {
				margin-top: 30rpx;
			}
		}
		
	}
</style>
